package org.restql.db.resource;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import javax.sql.DataSource;

import org.restql.db.Utility;
import org.restql.db.WhereClauseGenerator;

public abstract class ModifiableDatabaseResourceImpl implements ModifiableDatabaseResource {

	protected DataSource dataSource;
	protected List<String> select;
	protected String from;
	protected List<String> where = new ArrayList<String>();

	protected String dbName;
	protected String resourceName;
	protected String jdbcUrl;
	protected String jdbcDriver;
	protected String user;
	protected String password;

	protected boolean allowSelect = true;
	protected boolean allowInsert = true;
	protected boolean allowDelete = true;
	protected boolean allowUpdate = true;

	public String getConnectString() {
		return jdbcUrl + "||" + getUser() + "||" + getPassword();
	}

	public String getJdbcDriver() {
		return jdbcDriver;
	}

	public void setJdbcDriver(String jdbcDriver) {
		this.jdbcDriver = jdbcDriver;
	}

	public String getJdbcUrl() {
		return jdbcUrl;
	}

	public void setJdbcUrl(String jdbcUrl) {
		this.jdbcUrl = jdbcUrl;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public String getUser() {
		return user;
	}

	public void setUser(String user) {
		this.user = user;
	}

	public DataSource getDataSource() {
		return this.dataSource;
	}

	public void setFrom(String from) {
		this.from = from;
	}

	public void setWhere(List<String> where) {
		this.where = where;
	}

	public void setSelect(List<String> select) {
		this.select = select;
	}

	public void setDataSource(DataSource dataSource) {
		this.dataSource = dataSource;
	}
/*
	public String getFromString(RequestResource requestResource) {
		return "FROM " + this.from;
	}

	public String getSelectString(RequestResource requestResource) {
		return "SELECT " + Utility.commas(this.select) + " " +
		getFromString(requestResource) + " " +
		getWhereString(requestResource);
	}

	public String getWhereString(RequestResource requestResource) {
		String predicate = " ";
		if (this.where.size() > 0) {
			predicate += "WHERE " + Utility.ands(this.where);
		}
		return predicate + WhereClauseGenerator.generateWhereClause(requestResource.getQueryParameters(), false, connectString);
	}
*/
	public String getDbName() {
		return dbName;
	}

	public void setDbName(String dbName) {
		this.dbName = dbName;
	}

	public String getResourceName() {
		return resourceName;
	}

	public void setResourceName(String resourceName) {
		this.resourceName = resourceName;
	}

	public boolean allowDelete() {
		return this.allowDelete;
	}

	public boolean allowInsert() {
		return this.allowInsert;
	}

	public boolean allowSelect() {
		return this.allowSelect;
	}

	public boolean allowUpdate() {
		return this.allowUpdate;
	}

	public void setAllowDelete(boolean allowDelete) {
		this.allowDelete = allowDelete;
	}

	public void setAllowInsert(boolean allowInsert) {
		this.allowInsert = allowInsert;
	}

	public void setAllowSelect(boolean allowSelect) {
		this.allowSelect = allowSelect;
	}

	public void setAllowUpdate(boolean allowUpdate) {
		this.allowUpdate = allowUpdate;
	}

	public Connection getConnection(RequestResource requestResource) throws SQLException {
		return dataSource.getConnection();
	}
	/*
	public String getInsertString(RequestResource requestResource) {
		return "INSERT INTO " + this.from;
	}

	public String getUpdateString(RequestResource requestResource) {
		return "UPDATE " + this.from;
	}
	*/


}
